<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace  app\cardeadmin\controller;

use app\common\controller\AdminController;
use app\common\model\Operation;
use app\common\model\UserCashLog;
use http\Exception;
use think\Console;
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;

/**
 * 首页管理
 * Class Index
 * @package app\admin\controller
 */
class Index extends  AdminController {

    /**
     * 首页
     * @author 救火队队长
     */
    public function  index() {
        View::layout(false);
        $operationModel = new Operation();
        View::assign('menu', $operationModel->manageMenu(session('manage')['id']));
        return View::fetch();

    }

    /**
     * 欢迎页
     * @author 救火队队长
     */
    public function  welcome() {

        $home_list = Db::name("home_data")->where(['data'=>'updateAll'])->order('id desc')->find();
        //today data;
        $today_time         = getUtcTime();
        $today_start_time_1 = date('Y-m-d 00:00:00' , $today_time);
        $today_end_time_1   = date('Y-m-d H:i:s' , $today_time);
        $today_start_time   = strtotime($today_start_time_1);
        $today_end_time     = strtotime($today_end_time_1);
        //today --------------------------------------------------------------------------------------------------------
        //today --Total Tran Amount
        $tran_list_today    = UserCashLog::getTranData(" 
                and ( ctime between '{$today_start_time_1}' and '{$today_end_time_1}')");

        $home_list['auth_amount_today']         = getSprintf($tran_list_today['amount_auth']) ?? 0;
        $home_list['decline_amount_today']      = getSprintf($tran_list_today['amount_decline']) ?? 0;
        $home_list['post_amount_today']         = getSprintf($tran_list_today['amount_post']) ?? 0;
        $home_list['reversal_amount_today']     = getSprintf($tran_list_today['amount_reversal']) ?? 0;
        $home_list['refund_amount_today']       = getSprintf($tran_list_today['amount_credit']) ?? 0;
        //银3
        $home_list['auth_amount_today_lss']         = getSprintf($tran_list_today['amount_auth_lss']) ?? 0;
        $home_list['decline_amount_today_lss']      = getSprintf($tran_list_today['amount_decline_lss']) ?? 0;
        $home_list['post_amount_today_lss']         = getSprintf($tran_list_today['amount_post_lss']) ?? 0;
        $home_list['reversal_amount_today_lss']     = getSprintf($tran_list_today['amount_reversal_lss']) ?? 0;
        $home_list['refund_amount_today_lss']       = getSprintf($tran_list_today['amount_credit_lss']) ?? 0;

        //today -- number of card(当天新开卡数，注销卡数)
        $card_list_c   = Db::name('cards')
            ->where("(ctime between {$today_start_time} and {$today_end_time}) 
            and bank_type != 3")->count();
        $card_list_c_lss   = Db::name('cards')
            ->where("(ctime between {$today_start_time} and {$today_end_time}) 
            and bank_type = 3")->count();
        //今日创建用户
        $today_user_count = Db::name('user')->where("ctime between {$today_start_time} and {$today_end_time}")->count();
        //今日充值金额
        $funding_complete_today_credit = Db::name('fund_check')
            ->where("check_status = 1 and type = 0 and user_id !=1 and (utime between {$today_start_time} and {$today_end_time} )")->sum('amount');
        $funding_complete_today_debit = Db::name('fund_check')
            ->where("check_status = 1 and type = 1 and user_id !=1 and (utime between {$today_start_time} and {$today_end_time} )")->sum('amount');
        $funding_complete_today = $funding_complete_today_credit - $funding_complete_today_debit;

        $home_list['card_create_today']         = $card_list_c ?? 0;
        $home_list['card_create_today_lss']     = $card_list_c_lss ?? 0;
        $home_list['user_create_today']         = $today_user_count ?? 0;
        $home_list['funding_amount_today']      = $funding_complete_today ?? 0;

        View::assign($home_list);
        View::assign([
            'today_start_time_1'  => $today_start_time_1,
            'today_end_time_1'  => $today_end_time_1,
        ]);
        return View::fetch();
    }

    /**
     * 清除整站全部缓存
     * @author 救火队队长
     */
    public function clearCache()
    {
        Cache::clear();
        return redirect(url('index/welcome'));
    }

}
